package com.michaelbulava.TFS.vcs.utils;

import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import org.jetbrains.annotations.Nullable;

/**
 * Created by mbulava on 5/7/2015.
 */
public class TFSProgressUtil {

    public static void checkCanceled(ProgressIndicator indicator) throws ProcessCanceledException{
        if (indicator != null && indicator.isCanceled())
            throw new ProcessCanceledException();
    }

    public static void setProgressText(ProgressIndicator indicator, String text){
        if (indicator != null && text != null)
            indicator.setText(text);
    }

    public static void setProgressText2(@Nullable ProgressIndicator progressIndicator, @Nullable String text)
    {
        if ((progressIndicator != null) && (text != null)) {
            progressIndicator.setText2(text);
        }
    }

    public static void setIndeterminate(@Nullable ProgressIndicator progressIndicator, boolean indeterminate)
    {
        if (progressIndicator != null) {
            progressIndicator.setIndeterminate(indeterminate);
        }
    }

    public static void stop(@Nullable ProgressIndicator progressIndicator){
        if (progressIndicator != null)
            progressIndicator.stop();
    }

    public static boolean isCancelled(ProgressIndicator progressIndicator){
        if(progressIndicator != null)
            return progressIndicator.isCanceled();

        return false;
    }

    public static void setCancelled(ProgressIndicator progressIndicator){
        if (progressIndicator != null)
            progressIndicator.cancel();
    }

    public static void updateWorkedFraction(ProgressIndicator progressIndicator, int workedIncrement, int totalWork){
        if (progressIndicator != null ){
            if (progressIndicator.isIndeterminate() && totalWork > 0)
                progressIndicator.setIndeterminate(false);
            double addValue = (double)workedIncrement / (double)totalWork;
            double newValue = progressIndicator.getFraction() + addValue;
            progressIndicator.setFraction(newValue);
        }
    }


}
